<template>
  <div class="createPost-main-container-comment">
    <span v-show="evaluationDis">
      <el-divider><span
        style="color:#666;font-size:12px;"
      >以下为当前项目评论</span></el-divider></span>

    <!-- <div v-if="">该项目暂无评论</div> -->
    <el-scrollbar>
      <div>
        <div
          v-for="(item, index) in evaluationList"
          :key="index"
          class="no_evaluation"
        >
          <el-row :gutter="24" class="">
            <el-col :span="18">
              <span style="color:#2766b3">{{ item.username }}</span>
            </el-col>
            <el-col :span="6">
              <span
                class="evaluationTime"
              >{{ item.created_at }}&nbsp;评论</span>
            </el-col>
          </el-row>
          <el-row :gutter="24">
            <el-col :span="6" />
          </el-row>

          <span class="evaluationText">{{ item.text }}</span>
        </div>
      </div>
    </el-scrollbar>
    <!-- <div style="height: 30px"></div> -->
    <el-form class="comment_form" :model="evaluationFrom" label-width="80px" label-position="left">
      <el-form-item v-show="value == '3'" label="你的评价 *" class="label_per">
        <el-input v-model="evaluationFrom.text" type="tareare" />
      </el-form-item>
      <div
        v-show="value == '3'"
        class="footer"
        style="width:100%;text-align:center;padding:10px"
      >
        <el-button
          style="margin-left: 10px;border:1px silid red"
          type="success"
          @click="confirEvaluation"
        >评论</el-button>
        <router-link :to="'/project/index'">
          <el-button
            style="margin-left: 30px"
            size="medium"
            type="default"
          >返回</el-button>
        </router-link>
      </div>
    </el-form>
  </div>
</template>

<script>
import { getToken } from '@/utils/auth'
import { getInfo } from '@/api/user'
import { insertEvaluation, selectEvaluation } from '@/api/project'
// http://dc.tuobao.m.com/storage/images
export default {
  name: 'Audit',
  props: {
    is_comment: {
      type: Boolean,
      default: false
    }
  },
  data() {
    return {
      evaluationList: '',
      value: '',
      evaluationDis: false, // 是否显示评论条
      evaluationFrom: {
        project_id: '',
        text: '',
        username: '' // 评论人姓名
      } // 项目评价表单内容
    }
  },
  computed: {},
  created() {
    if (this.is_comment) {
      const value = this.$route.query.value
      const id = this.$route.query && this.$route.query.id
      this.evaluationFrom.project_id = id
      this.value = value
      this.selectEvaluation(id)
      this.getUserInfo()
    }
  },
  methods: {
    // 提交评价
    confirEvaluation() {
      new Promise((resolve, reject) => {
        insertEvaluation({ evaluationFrom: this.evaluationFrom })
          .then(response => {
            this.$message({
              type: 'success',
              message: '评论成功!'
            })
          })
          .then(() => {
            this.selectEvaluation(this.evaluationFrom.project_id)
            this.evaluationFrom.text = ''
          })
      })
    },
    // 查询评论内容
    selectEvaluation(id) {
      new Promise((resolve, reject) => {
        selectEvaluation({ project_id: id }).then(response => {
          this.evaluationList = response.data[0].projects_belongs_to_many
          if (this.evaluationList.length == 0) {
            this.evaluationDis = false
          } else {
            this.evaluationDis = true
          }
        })
      })
    },
    // 获取当前用户的登录信息
    getUserInfo() {
      new Promise((resolve, reject) => {
        getInfo().then(response => {
          this.evaluationFrom.username = response.data.name // 当前登录用户的姓名
        })
      })
    }
  }
}
</script>

<style>
.createPost-main-container-comment {
  padding: 0px 45px 0px 50px;
}
.createPost-main-container-comment .el-timeline-item__content .el-card__body {
  padding: 0px;
  padding-left: 20px;
}
.createPost-main-container-comment .no_evaluation {
  background: rgb(245, 245, 245);
  border-radius: 5px;
  padding: 10px 60px 10px 60px;
  margin-bottom: 12px;
}
.createPost-main-container-comment .el-scrollbar .el-scrollbar__wrap {
  overflow-x: hidden;
  max-height: 300px;
  padding: 10px;
  /* height: 300px; */
}
.createPost-main-container-comment .evaluationText {
  padding: 10px 0px 10px 0px;
  border-bottom: 1px dashed #dedede;
  display: block;
  font-size: 14px;
  font-family: "Microsoft YaHei";
}
.createPost-main-container-comment .evaluationTime {
  color: #888;
  text-align: right;
  padding-right: 10px;
  font-size: 13px;
}
/* .createPost-main-container-comment .comment_form{
  padding: 20px;
} */
</style>
